//
//  ContactMan.m
//  考试
//
//  Created by ZF on 16/6/16.
//  Copyright © 2016年 ZhuFeng. All rights reserved.
//

#import "ContactMan.h"

@implementation ContactMan
- (void)setterName:(NSString *)name
{
    _name = name;
}

- (void)setterPhone:(NSString *)phone
{
    _phone = phone;
}

- (void)setterDepartment:(NSString *)department
{
    _department = department;
}

- (NSString *)name
{
    return _name;
}

- (NSString *)phone
{
    return _phone;
}

- (NSString *)department
{
    return _department;
}


- (void)parsingVcf
{
    NSString *path = @"/Volumes/电脑店U盘/朱峰/00002.vcf";
    
    NSString *vcf = [[NSString alloc] initWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
    NSArray *arr = [vcf componentsSeparatedByString:@"\n"];
    
    NSMutableArray *muArr = [NSMutableArray new];
    ContactMan *man;
    
    for(NSString *line in arr)
    {
        if([line hasPrefix:@"BEGIN"])
        {
            man= [ContactMan new];
        }
        
        if([line hasPrefix:@"END"])
        {
            [muArr addObject:man];
        }
        
        if([line hasPrefix:@"FN"])
        {
            NSArray *arr1 = [line componentsSeparatedByString:@":"];
            
           NSString *str = [arr1[1] stringByReplacingOccurrencesOfString:@"=" withString:@"%"];
            str = [str stringByRemovingPercentEncoding];
            man.name = str;
             NSLog(@"%@",str);
        }
        
        if([line hasPrefix:@"TEL"])
        {
            NSArray *arr2 = [line componentsSeparatedByString:@":"];
            man.phone = arr2[1];
             NSLog(@"%@",arr2[1]);
        }
        
        if([line hasPrefix:@"ORG"])
        {
            NSArray *arr3 = [line componentsSeparatedByString:@":"];
            NSString *str = [arr3[1] stringByReplacingOccurrencesOfString:@"=" withString:@"%"];
            str = [str stringByRemovingPercentEncoding];
            man.department = str;
             NSLog(@"%@",str);
        }
    }
    self.data = muArr;
   
}


- (id)initWithName:(NSString *)name AndPhone:(NSString *)phone AndDepartment:(NSString *)department
{
    self = [super init];
    if(self)
    {
        name = _name;
        phone = _phone;
        department = _department;
    }
    return self;
}


- (void)addContact
{
    NSMutableArray *arry = [NSMutableArray new];
    NSMutableDictionary *dict;
    ContactMan *man;
    for(man in self.data)
    {
        dict[@"department"] = man.department;
       
        dict[@"phone"] = man.phone;
        //NSLog(@"%@",man.name);
        
        dict[@"name"] = man.name;
        //NSLog(@"%@",man.name);
        dict = [NSMutableDictionary new];
        [arry addObject:dict];
    }
    
    BOOL b = [arry writeToFile:@"/Volumes/电脑店U盘/tel.plist" atomically:YES];
    
    if(b)
    {
        NSLog(@"输出成功");
    }
    else NSLog(@"输出失败");
}







@end
